package com.fbli.test.service.impl;

import com.fbli.test.common.entity.project.ProjectAddReqMsg;
import com.fbli.test.common.entity.project.ProjectReqConditionQuery;
import com.fbli.test.common.entity.project.ProjectResMsg;
import com.fbli.test.entity.Project;
import com.fbli.test.mapper.ProjectMapper;
import com.fbli.test.service.ProjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    ProjectMapper projectMapper;

    @Override
    public ProjectResMsg getProjects(long userId, int page, int size) {
        if (page <= 0 || size <= 0) return null;
        //分页开始
        PageHelper.startPage(page, size);
        List<Project> projects = projectMapper.getProjects(userId,-1);
        //返回分页后的结果
        PageInfo<Project> info = new PageInfo<>(projects);
        ProjectResMsg projectResMsg = new ProjectResMsg();
        projectResMsg.setTotal(info.getTotal());
        projectResMsg.setInfoList(info.getList());
        return projectResMsg;
    }

    @Override
    public List<Project> getById(long userId, long projectId) {
        return projectMapper.getProjects(userId,projectId);
    }

    @Override
    public boolean addProject(long userId, ProjectAddReqMsg msg) {
        try {
            Project projectEntity = new Project();
            projectEntity.setUserId(userId);
            projectEntity.setName(msg.getName());
            projectEntity.setDesc(msg.getDesc());
            projectEntity.setTime(System.currentTimeMillis());
            projectMapper.insertSelective(projectEntity);
        } catch (Exception e) {
//            log.error("添加项目出错！ e=" + e);
            return false;
        }
        return true;
    }

    @Override
    public boolean editProject(long userId, ProjectAddReqMsg msg) {
        Project projectEntity = projectMapper.findFirstByIdAndUserIdAndDeletedFalse(userId,msg.getProjectId());
        if (projectEntity == null) return false;
        try {
            projectEntity.setName(msg.getName());
            projectEntity.setDesc(msg.getDesc());
            projectEntity.setEditTime(System.currentTimeMillis());
            projectMapper.updateByPrimaryKey(projectEntity);
        } catch (Exception e) {
//            log.error("编辑项目出错！ e=" + e);
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(long userId, List<Long> projectIdList) {
        try {
            projectMapper.delete(userId, projectIdList);
        } catch (Exception e) {
//            log.error("删除项目出错！ e=" + e);
            return false;
        }
        return true;
    }

    @Override
    public ProjectResMsg selectByIdOrByName(ProjectReqConditionQuery query) {
        //开始分页
        PageHelper.startPage(query.getPage(),query.getSize());
        List<Project> projects = projectMapper.selectByIdOrByName(query.getCondition());
        //返回分页后的结果
        PageInfo<Project> info = new PageInfo<>(projects);
        ProjectResMsg projectResMsg=new ProjectResMsg();
        projectResMsg.setTotal(info.getTotal());
        projectResMsg.setInfoList(info.getList());
        return projectResMsg;
    }
}
